package net.zhuike.novel.app;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.support.multidex.MultiDex;
import android.support.v7.app.AppCompatDelegate;
import android.util.Log;

import com.luomi.lm.ad.DRAgent;
import com.tendcloud.tenddata.TCAgent;
import com.umeng.commonsdk.UMConfigure;


import net.zhuike.novel.app.service.DownloadBService;
import net.zhuike.novel.app.service.UpdateConfigService;
import net.zhuike.novel.app.ui.activity.AdsActivity;
import net.zhuike.novel.app.ui.base.Constant;
import net.zhuike.novel.app.ui.base.CrashHandler;
import net.zhuike.novel.app.ui.utils.AppUtils;
import net.zhuike.novel.app.ui.utils.Foreback;
import net.zhuike.novel.app.ui.utils.SharedPreferencesUtil;
import net.zhuike.novel.app.ui.utils.ToastUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import framework.common.timer.TimeButton;

public class ZhuiKeApplication extends Application implements Foreback.Listener{
    private static Context context;
    public static Map<String, Long> map = new HashMap<String, Long>();;//用来存放倒计时的时间
    private static ZhuiKeApplication sInstance;
    private long lenght = 30 * 1000;// 倒计时长度,这里给了默认60秒
    private Timer t;
    private TimerTask tt;
    private long time;
    @Override
    public void onCreate() {
        super.onCreate();
        context=this;
        sInstance = this;
        AppUtils.init(context);
        CrashHandler.getInstance().init(this);
        MultiDex.install(this);
        Foreback.init(this);
        Foreback.registerListener(this);
        UMConfigure.init(this, "5a4b8bb2f43e481c1a000476", "Umeng", UMConfigure.DEVICE_TYPE_PHONE, "");
        TCAgent.LOG_ON=true;
        // App ID: 在TalkingData创建应用后，进入数据报表页中，在“系统设置”-“编辑应用”页面里查看App ID。
        // 渠道 ID: 是渠道标识符，可通过不同渠道单独追踪数据。
        TCAgent.init(this);
        // 如果已经在AndroidManifest.xml配置了App ID和渠道ID，调用TCAgent.init(this)即可；或与AndroidManifest.xml中的对应参数保持一致。
        TCAgent.setReportUncaughtExceptions(true);
        DRAgent.getInstance().init(this.getApplicationContext(), "293417823495e6c484d15db01290f63e", true);
        initPrefs();
        initNightMode();
        Intent intent = new Intent(this, UpdateConfigService.class);
        startService(intent);
        startService(new Intent(context, DownloadBService.class));
    }
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
    public static ZhuiKeApplication getsInstance() {
        return sInstance;
    }

    /**
     * 初始化SharedPreference
     */
    protected void initPrefs() {
        SharedPreferencesUtil.init(getApplicationContext(), getPackageName() + "_preference", Context.MODE_MULTI_PROCESS);
    }

    protected void initNightMode() {
        boolean isNight = SharedPreferencesUtil.getInstance().getBoolean(Constant.ISNIGHT, false);
        if (isNight) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        } else {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        }
    }
    public static Context getAppContext() {
        return ZhuiKeApplication.context;
    }

    @Override
    public void onApplicationEnterForeground(Activity activity) {//后台进入前台
        Date  date=new Date();
        date.getTime();
        Long date2=map.get("time1");
        if(date2!=null) {
            if ((date.getTime() - date2) / 1000 > 30) {
                startActivity(new Intent(this, AdsActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
            }
        }
    }

    @Override
    public void onApplicationEnterBackground(Activity activity) {//前台进入后台
        Date  date=new Date();
        map.put("time1",date.getTime());
    }
}
